<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Test extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function test()
    {
        
        $weekAgoTime = strtotime("-1 week");//一周前时间戳
        // $weekAgoTime = time();
        
        Db::name('admin_log')->where(['createtime'=>['<',$weekAgoTime]])->delete();
        
        
        Db::name('mental_testlog')->where(['createtime'=>['<',$weekAgoTime],'status'=>'unfinish'])->delete();
        
        
        Db::name('mental_order')->where(['createtime'=>['<',$weekAgoTime],'status'=>'created'])->delete();
        
        
        $list = Db::name('mental_testlog')->where(['createtime'=>['<',$weekAgoTime],'status'=>'finish','isOrder'=>0])->select();
        foreach($list as $k=>$v){
            $isOrder = Db::name('mental_order')->where(['test_id'=>$v['id'],'status'=>'paid'])->find();
            if ($isOrder||$v['invitation_code']!=='用户未填写') {
                Db::name('mental_testlog')->where('id',$v['id'])->update(['isOrder'=>1]);
            }else {
                Db::name('mental_testlog')->where('id',$v['id'])->delete();
            }
            
        }
        
        $this->getSql('fa_admin_log');
        $this->getSql('fa_mental_testlog');
        $this->getSql('fa_mental_order');
        
        return 1;
    }
    
    public function getSql($str)
    {
        $mysql_server="localhost";
        $mysql_username="47_122_9_222";
        $mysql_password="mYTktTaGxNMiwfMA";
        $mysql_database="47_122_9_222";
        //建立数据库链接
        $conn = mysqli_connect($mysql_server,$mysql_username,$mysql_password) or die("数据库链接错误");
        //选择某个数据库
        mysqli_select_db($conn,$mysql_database);

        mysqli_query($conn,"CHECK TABLE `$str`");
        
        mysqli_query($conn,"ALTER TABLE `$str` ENGINE = INNODB");
        
        mysqli_query($conn,"ANALYZE TABLE `$str`");
        
        mysqli_query($conn,"OPTIMIZE TABLE `$str`");
        
        return true;
    }
    
}
